package server;

import java.io.*;
import java.util.Arrays;

import msgs.*;

/**
 * Parses the client's actions out
 * Team 9: Andrew Hayworth, Brian Parrella, Ryan Kortmann, Nina Papa
 * @author Andrew Hayworth
 */
public class ClientParser extends Thread implements TTConstants {
	private ClientMsg cmsg;
	private boolean go;
	
	public ClientParser() {
		go = true;
	}
	
	public void run() {
		while (go) {
			if(!(ClientListener.inQ.isEmpty())){
				cmsg = ClientListener.inQ.remove(0);
				System.out.println(cmsg.getAction());
				System.out.println(cmsg.getName());
				// constantly update when we last heard from them ....
				if (!(TTServer.lastTalkTime.containsKey(cmsg.getName()))) {
					TTServer.lastTalkTime.put(cmsg.getName(), System.currentTimeMillis());
				}
				else {
					TTServer.lastTalkTime.remove(cmsg.getName());
					TTServer.lastTalkTime.put(cmsg.getName(), System.currentTimeMillis());
				}
				switch(cmsg.getAction()) {
				case LOGIN:
					(new NewClientConnection(cmsg)).start();
					break;
				case LOGOUT:
					(new ClientRemover(cmsg)).start();
					break;
				case QUERY_GROUP:
				case QUERY:
					(new ClientQuerier(cmsg)).start();
					break;
				case BLOCK:
				case UNBLOCK:
					(new Blocker(cmsg)).start();
					break;
				case BL_UPDATE:
				case WANT_BL:
					(new BLWorker(cmsg)).start();
					break;
				case PROFILE_REQUEST:
				case PROFILE_UPDATE:
					(new ProfileWorker(cmsg)).start();
					break;
				case PM_USER:
					(new PMHandler(cmsg)).start();
					break;
				case NEW_CHAT:
				case JOIN_CHAT:
				case KILL_CHAT:
				case CHAT_UPDATE:
				case LEAVE_CHAT:
					(new ChatHandler(cmsg)).start();
					break;
				case PM_LOG_UPDATE:
					(new Thread() {
						public void run() {
							try {
								// apparently I did implement this on server side. update the log for the pm requested
								// not going to be implemented on the client side though
								File f = new File("data/pmlogs/" + cmsg.getName() + cmsg.getUser() + ".log");
								if (!(f.exists())) f.mkdirs();
								PrintWriter p = new PrintWriter(new FileWriter(f));
								p.println(cmsg.getName() + ":" + cmsg.getMsg());
								p.flush();
								p.close();
							}
							catch (IOException e) {
								e.printStackTrace();
							}
						}
					}).start();
					break;
				case ADMIN_LIST_CHATS:
					(new Thread() {
						public void run() {
							if (!(Arrays.equals(cmsg.getClientKey(), TTServer.connTable.get(cmsg.getName()).getClientKey()))) {
								return;
							}
							else {
								// cool, tell the admins who they're talking to
								AdminSender.outQ.add((AdminMsg) new AdminRequestMsg(TTServer.adminTable.get(cmsg.getUser()), AdminActions.LIST_USER_CHATS, cmsg.getName(), cmsg.getChats()));
							}
						}
					}).start();
					break;
				case KEEP_ALIVE:
					// nothing to see here, move along
					break;
				case OFFLINE_MSG:
					(new PMHandler(cmsg)).start();
					break;
				}
			}
			else {
				try {
					Thread.sleep(100);	// sleep if we have nothing to do
				}
				catch (InterruptedException e) {
					e.printStackTrace(); 
				}
			}
		}
	}
	
	/**
	 * Kills the thread nastily
	 */
	public void killClientParser() {
		go = false;
	}
}
